package com.hfjava.ebook.server.impl;

import com.hfjava.ebook.entity.Book;
import com.hfjava.ebook.entity.Pageable;
import com.hfjava.ebook.mapper.BookMapper;
import com.hfjava.ebook.server.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional(rollbackFor = {Exception.class},readOnly = false)
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class},readOnly = true,propagation = Propagation.SUPPORTS)
    public Pageable findByPage(Integer categoryId, int page, int size,String sortField,int sortDir) {
        List<Book> bookList = bookMapper.getBookByPage(categoryId,page,size,sortField,sortDir);
        int count = bookMapper.getBookTotalCount(categoryId);
        Pageable pageable = new Pageable(count,page,size,bookList);
        return pageable;
    }

    @Override
    public int saveBook(Book book) {
        return bookMapper.addBook(book);
    }

    @Override
    public int modifyBook(Book book) {
        return bookMapper.updateBook(book);
    }

    @Override
    public int deleteBookById(Integer bid) {
        return bookMapper.deleteBookById(bid);
    }

    @Override
    public Book findBookById(Integer bid) {
        return bookMapper.getBookById(bid);
    }
}
